#include "StdAfx.h"
#include "ClientReadStrategy.h"
#include "RdcTransport.h"


void rdc_transport::ReadStrategy::OnData(LPVOID pData, UINT32 dataLength, UINT32 totalLength, UINT32 dataFlags)
{
    try
    {
        if (dataFlags & CHANNEL_FLAG_FIRST)
        {
            m_pReadBuffer.resize(0);
            m_pReadBuffer.reserve(totalLength);
        }

        const char* tmpBuf = reinterpret_cast<char*>(pData);
        m_pReadBuffer.insert(m_pReadBuffer.end(), tmpBuf, tmpBuf + dataLength);

        if (dataFlags & CHANNEL_FLAG_LAST)
        {
            m_readHandler->OnRead(&m_pReadBuffer);
        }
    }
    catch(std::exception& ex)
    {
        std::stringstream msg;
        msg << __FUNCTION__<<" "<<ex.what();
        m_readHandler->OnReadFail(msg.str().c_str());
    }
    catch(...)
    {
        std::stringstream msg;
        msg << __FUNCTION__<<" unknown error";
        m_readHandler->OnReadFail(msg.str().c_str());
    }
}

void rdc_transport::ReadStrategy::SetReadHandler(IReadHandler* readHandler)
{
    assert(readHandler);

    m_readHandler = readHandler;
}